package com.wang.tingge.servlet.user;

import com.wang.tingge.service.UserService;
import com.wang.tingge.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/studio/user/register.do")
public class RegisterDoServlet extends HttpServlet {
    //由于表示过程的对象一般都是单例，定义成属性，减少对象的创建，相对来说性能好一点
    private final UserService userService=new UserService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.读取用户名和密码
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        //2.完成注册操作（密码加密+insert表，放在service中处理）

        //由于这个用户对象要放在session中，属于表现得对象，所以使用view_object
        UserVO userVO=userService.register(username,password);

        //3.注册之后进行登录，将用户对象放入session中
        HttpSession session=req.getSession();
        session.setAttribute("currentUser",userVO);

        //4.重定向到创作中心的首页
        resp.sendRedirect("/studio/");
    }
}
